//
//  SecondController.m
//  ProjectA
//
//  Created by lanouhn on 15/5/30.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "SecondController.h"
#import "FirstCenterCell.h"
#import "LoadingTableViewCell.h"
#import "NetWorkEngine.h"
#import "CellController.h"
#import "ONLY.h"

#define kURL @"http://lol.video.luckyamy.com/api/?cat=hero&id=%@&page=%d&ap=lolg&ver=1.1"

@interface SecondController ()<NetworkEngineDelegate>
@property (nonatomic ,retain)UILabel *alable ;
@property (nonatomic ,retain)NSMutableArray *dataSource;
@property (nonatomic ,assign)int pageNumber;
-(void)_startLoadingWithPage:(int)page;
@end

@implementation SecondController
- (UILabel *)alable {
    if (!_alable) {
        self.alable = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)]autorelease];
        self.alable.text = self.tittle;
        self.alable.textAlignment = NSTextAlignmentCenter;
        self.alable.textColor = [UIColor whiteColor];
    }
    return _alable;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.titleView = self.alable;
   
    [self.tableView registerClass:[FirstCenterCell class] forCellReuseIdentifier:@"CELL"];
    [self.tableView registerClass:[LoadingTableViewCell class] forCellReuseIdentifier:@"CALL"];
    
    self.refreshControl = [[[UIRefreshControl alloc]init]autorelease];
    [self.refreshControl addTarget:self action:@selector(handleResh:) forControlEvents:UIControlEventValueChanged];
    self.pageNumber = 1;
    [self _startLoadingWithPage:self.pageNumber];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}
- (void)handleResh:(UIRefreshControl *)sender{
    self.pageNumber = 1;
    [self _startLoadingWithPage:self.pageNumber];
}
-(void)_startLoadingWithPage:(int)page {
    NetworkEngine *engine = [NetworkEngine engineWithURL:[NSURL URLWithString:[NSString stringWithFormat:kURL,self.ID,self.pageNumber]] parameters:nil delegate:self];
    [engine start];
}
- (void)networkDidFinishLoading:(NetworkEngine *)engine withInfo:(id)info{
    [self.refreshControl endRefreshing];
    if (self.pageNumber == 1) {
        [self.dataSource removeAllObjects];
    }
    NSArray *array = [NSJSONSerialization JSONObjectWithData:info options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *dic in array) {
        FirstModel *model = [FirstModel modleWithDictionary:dic];
        [self.dataSource addObject:model];
    }
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.dataSource.count ;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.dataSource.count) {
        return [tableView dequeueReusableCellWithIdentifier:@"CALL" forIndexPath:indexPath];
    }
    FirstCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    [cell cellWithModel:self.dataSource[indexPath.row] temp:NO];
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.dataSource.count) {
        return 30;
    }
    return 110;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CellController *cellVC = [[CellController alloc]init];
    cellVC.model = self.dataSource[indexPath.row];
    [[ONLY creatOnly] passingValueWithContentmodel:self.dataSource[indexPath.row]];
    [self.navigationController pushViewController:cellVC animated:YES];
    [cellVC release];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
